package com.bed.lovewithpatientbed.service.impl;

import com.bed.lovewithpatientbed.common.bean.LayUiPageBean;
import com.bed.lovewithpatientbed.dao.EmployeeDao;
import com.bed.lovewithpatientbed.dao.RoleDao;
import com.bed.lovewithpatientbed.dao.WorkerMessageDao;
import com.bed.lovewithpatientbed.pojo.*;
import com.bed.lovewithpatientbed.service.EmployeeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author myp
 * @version 1.0.0
 * @ClassName EmployeeServiceImpl.java
 * @Description 雇员Service的实现类
 */
@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeDao employeeDao;
    @Resource
    private WorkerMessageDao workerMessageDao;
    @Resource
    private RoleDao roleDao;



    /**
     * @title 增加雇员
     * @description 初始雇员软删除默认为1；
     * @author myp
     * @updateTime
     * @throws
     */
    @Override
    @Transactional
    public Integer saveEmployee(Employee employee) {
        // 先添加 workmessage
        WorkerMessage workerMessage=new WorkerMessage();
        workerMessage.setWorkMessagePhone(employee.getEmployeePhone());
        workerMessage.setWorkMessagePassword("123456");
        workerMessage.setRoleId(employee.getRole().getRoleId());
        workerMessage.setSoftdel(1);
        workerMessageDao.insertSelective(workerMessage);
        WorkerMessageExample example=new WorkerMessageExample();
        WorkerMessageExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(employee.getRole().getRoleId());
        criteria.andWorkMessagePhoneEqualTo(employee.getEmployeePhone());
        List<WorkerMessage> workerMessages = workerMessageDao.selectByExample(example);
        // 获取id
        employee.setWorkerMessageId(workerMessages.get(0).getWorkerMessageId());
        // emp
        employee.setSoftdel(1);
        return employeeDao.insert(employee);
    }
    /**
     * @title 删除雇员
     * @description 删除雇员使用软删除，软删除变为0；
     * @author myp
     * @updateTime
     * @throws
     */
    @Override
    public Integer delEmployee(Employee employee) {
        employee.setSoftdel(0);
        return employeeDao.updateByPrimaryKeySelective(employee);
    }

    /**
     * @title 修改雇员信息
     * @description
     * @author myp
     * @updateTime
     * @throws
     */
    @Override
    public Integer updateEmployee(Employee employee) {
        return employeeDao.updateByPrimaryKeySelective(employee);
    }

    /**
     * @title 查找雇员列表
     * @description 按照条件组合查询，按照名字模糊查询、身份查询、入职时间查询、分页查询
     * @author myp
     * @updateTime
     * @throws
     */
    @Override
    public List<Employee> selectEmployeeList(LayUiPageBean pageBean, Employee employee , Date staData , Date endDate) {
        EmployeeExample employeeExample=new EmployeeExample();
        EmployeeExample.Criteria employeeExampleCriteria = employeeExample.createCriteria();
        //名字模糊查询
        if (employee.getEmployeeName() != null && !"".equals(employee.getEmployeeName())){
            employeeExampleCriteria.andEmployeeNameLike("%"+employee.getEmployeeName()+"%");
        }
        //雇员种类查询，分为维修人员和财务人员;根据工作人员信息表寻找;
        if(employee.getWorkerMessageId()!=null && !"".equals(employee.getWorkerMessageId())){
            employeeExampleCriteria.andWorkerMessageIdEqualTo(employee.getWorkerMessageId());
        }
        //按照入职时间查询
        if(staData != null && !"".equals(staData)){
            employeeExampleCriteria.andEmployeeDateGreaterThanOrEqualTo(staData);
        }
        if(endDate != null && !"".equals(endDate)){
            employeeExampleCriteria.andEmployeeDateLessThanOrEqualTo(endDate);
        }
        //判断页面是否空
        if(pageBean.getLimit() != null
                && !"".equals(pageBean.getLimit() )
                && pageBean.getPage() != null
                && !"".equals(pageBean.getPage())){
            employeeExample.setOffset((pageBean.getPage() - 1) * pageBean.getLimit());
            employeeExample.setLimit(pageBean.getLimit().intValue());
        }
        employeeExampleCriteria.andSoftdelEqualTo(1);
        List<Employee> employeeList = employeeDao.selectByExample(employeeExample);
        //嵌套查询
        for(int i=0;i<employeeList.size();i++){
            employeeList.set(i,setClass(employeeList.get(i)));
        }
        return employeeList;
    }
    /**
     * @title 查找一个雇员
     * @description 根据雇员编号查找
     * @author myp
     * @updateTime
     * @throws
     */
    @Override
    public Employee selectEmployee(Integer employeeId) {
        Employee employee = setClass(employeeDao.selectByPrimaryKey(employeeId));
        return employee;
    }

    @Override
    public Integer getCount(Employee employee,  Date staData , Date endDate) {
        EmployeeExample employeeExample=new EmployeeExample();
        EmployeeExample.Criteria employeeExampleCriteria = employeeExample.createCriteria();
        //名字模糊查询
        if (employee.getEmployeeName() != null && !"".equals(employee.getEmployeeName())){
            employeeExampleCriteria.andEmployeeNameLike("%"+employee.getEmployeeName()+"%");
        }
        //雇员种类查询，分为维修人员和财务人员;根据工作人员信息表寻找;
        if(employee.getWorkerMessageId()!=null && !"".equals(employee.getWorkerMessageId())){
            employeeExampleCriteria.andWorkerMessageIdEqualTo(employee.getWorkerMessageId());
        }
        //按照入职时间查询
        if(staData != null && !"".equals(staData)){
            employeeExampleCriteria.andEmployeeDateGreaterThanOrEqualTo(staData);
        }
        if(endDate != null && !"".equals(endDate)){
            employeeExampleCriteria.andEmployeeDateLessThanOrEqualTo(endDate);
        }
        Long count = employeeDao.countByExample(employeeExample);
        return count.intValue();
    }

    /**
     * @title 放置嵌套信息
     * @description 
     * @author myp 
     * @updateTime   
     * @throws 
     */
    public Employee setClass(Employee employee){

        //加入员工信息
        employee.setWorkerMessage(workerMessageDao.selectByPrimaryKey(employee.getWorkerMessageId()));
        //加入角色信息
        employee.setRole(roleDao.selectByPrimaryKey(workerMessageDao.selectByPrimaryKey(employee.getWorkerMessageId()).getRoleId()));
        return  employee;
    }
}